//
//  HttpHelper.m
//  UCLBRTStatic
//  网络请求
//  Created by myApple on 16/2/23.
//  Copyright (c) 2016年 myApple. All rights reserved.
//

#import "HttpHelper.h"
#import "Constants.h"
//#import "Reachability.h"

@interface HttpHelper()<NSURLConnectionDelegate,NSURLConnectionDataDelegate>
{
    NSMutableData * downLoadData;
}

@end

@implementation HttpHelper

-(id)init
{
    self = [super init];
    if (self) {
        downLoadData =[[NSMutableData alloc]init];
    }
    return self;
}

#pragma mark ------------- 网络请求
-(void)postRequest:(NSString *)Url RequestParams:(NSDictionary *)params
{
    NSURL *url = [NSURL URLWithString:Url];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
    NSString * parseParamResult = [self parseParams:params];
    NSData * postData = [parseParamResult dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
    
   
   NSURLConnection * connection = [NSURLConnection connectionWithRequest:request delegate:self];
    [connection start];
    
 
    
}


#pragma mark ----------  成功链接完全结束
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (self.resultBlock) {
        self.resultBlock(downLoadData,nil);
    }
}

#pragma mark --------- 接受的数据
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [downLoadData appendData:data];
}


#pragma mark ----------- 断网 链接不上 超时等错误
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    if (self.resultBlock) {
        self.resultBlock(nil,error);
    }
}

#pragma mark --------- 处理https的字签名的问题
-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    
    if ([challenge.protectionSpace.authenticationMethod isEqualToString: NSURLAuthenticationMethodServerTrust])
    {
        
        NSURL* baseURL = [NSURL URLWithString:HEADERURL];
                          
        if ([challenge.protectionSpace.host isEqualToString:baseURL.host])
        {
            SecTrustRef trust = challenge.protectionSpace.serverTrust;
                              
            NSURLCredential *cred = [NSURLCredential credentialForTrust:trust];
                              
            [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
                              
        }
    }
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
    
}

#pragma mark --------------把NSDictionary解析成post格式的NSString字符串
-(NSString *)parseParams:(NSDictionary *)params{
    NSString *keyValueFormat;
    NSMutableString *result = [NSMutableString new];
    //实例化一个key枚举器用来存放dictionary的key
    NSEnumerator *keyEnum = [params keyEnumerator];
    id key;
    while (key = [keyEnum nextObject]) {
        keyValueFormat = [NSString stringWithFormat:@"%@=%@&",key,[params valueForKey:key]];
        [result appendString:keyValueFormat];
    }
    return result;
}

//#pragma mark -------------- 判断是否有网络
//-(BOOL)IsHaveNetwork
//{
//    Reachability * reach = [Reachability reachabilityForInternetConnection];
//    if([reach currentReachabilityStatus] == NotReachable) {
//        return NO;
//    }
//    return YES;
//}

@end
